package com.didojoy.lame4android;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.didojoy.lameliarary.Mp3Recorder;
import com.didojoy.lameliarary.listener.RecorderListener;
import com.didojoy.lameliarary.utils.Mp3Util;

import java.io.File;

public class MainActivity extends AppCompatActivity implements RecorderListener {
    private Mp3Recorder mMp3Recorder;
    private StringBuilder mStringBuilder;
    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.tv_tip);

        findViewById(R.id.btn_start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mStringBuilder = new StringBuilder();
                mStringBuilder.append("start recording" + "\n");
                mTextView.setText(mStringBuilder.toString());

                mMp3Recorder = new Mp3Recorder(MainActivity.this);
                File file = getPCMFile();
                mMp3Recorder.start(file);
            }
        });

        findViewById(R.id.btn_stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMp3Recorder != null) {
                    mMp3Recorder.stop();
                }

                mStringBuilder.append("stop recording" + "\n");
                mTextView.setText(mStringBuilder.toString());

            }
        });
        findViewById(R.id.btn_mp3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mPcmPath)) {
                    return;
                }

                File file = new File(mPcmPath);
                File mp3 = new File(file.getParent(), file.getName().replace(".pcm", ".mp3"));
                boolean isConvert = Mp3Util.getInstance().convertRaw2Mp3(mPcmPath, mp3.getAbsolutePath());

                if (isConvert) {
                    mStringBuilder.append("mp3 path: " + mp3.getAbsolutePath() + "\n");
                } else {
                    mStringBuilder.append("mp3 path: error" + "\n");
                }
                mTextView.setText(mStringBuilder.toString());
            }
        });
    }

    public File getPCMFile() {
        File directory = Environment.getExternalStoragePublicDirectory("1Recorder");
        if (!directory.exists()) {
            directory.mkdirs();
        }
        return new File(directory, System.currentTimeMillis() + ".pcm");
    }

    private String mPcmPath;

    @Override
    public void onRecorderSucc(final String path) {
        mPcmPath = path;

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mStringBuilder.append("pcm path: " + path + "\n");
                mTextView.setText(mStringBuilder.toString());
            }
        });
    }

    @Override
    public void onRecorderError(Exception e) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mStringBuilder.append("pcm path: error" + "\n");
                mTextView.setText(mStringBuilder.toString());
            }
        });
    }
}
